import 'package:flutter/material.dart';
import 'constant.dart' show Appcolors, IntValues, Device, Constants;
import 'ChatDataList.dart' show mockChatListData, Conversation;

class ChatListPage extends StatelessWidget {
  final Conversation wechatConversation;
  BuildContext context;

  ChatListPage({Key key, this.wechatConversation})
      : assert(wechatConversation != null);

  @override
  Widget build(BuildContext context) {
    Widget avatarImg;
    this.context = context;
    if (!wechatConversation.isFromNet()) {
      avatarImg = Image.asset(wechatConversation.avatar,
          width: IntValues.AvatarImgSize,
          height: IntValues.AvatarImgSize,
          fit: BoxFit.fill);
    } else {
      avatarImg = Image.network(
        wechatConversation.avatar,
        width: IntValues.AvatarImgSize,
        height: IntValues.AvatarImgSize,
        fit: BoxFit.fill,
      );
    }
    return _ChatItemView(avatarImg);
  }

  _unReadMsgView(String count) {
    return Container(
      alignment: Alignment.center,
      width: 16,
      height: 16,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10), color: Colors.red),
      child: Text(
        "$count",
        style: TextStyle(fontSize: 12, color: Colors.white),
      ),
    );
  }

  Widget _avatarContainer(Widget avatarImg) {
    var unreadMsgCount = wechatConversation.unreadMsgCount;
    String newCount = "$unreadMsgCount";
    if (unreadMsgCount > 0) {
      if (unreadMsgCount > 99) {
        newCount = "99+";
      }
      return Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          avatarImg,
          Positioned(
            right: -5.0,
            top: -5.0,
            child: _unReadMsgView(newCount),
          ),
        ],
      );
    } else {
      return avatarImg;
    }
  }

  Widget _ChatItemView(Widget avatarImg) {
    return GestureDetector(
      onTap: () {
        print(wechatConversation.title);

        Scaffold.of(context).showSnackBar(new SnackBar(
            content: Text(wechatConversation.title),
            duration: Duration(milliseconds: 1000)));
      },
      child: Container(
        decoration: BoxDecoration(
            color: Color(Appcolors.ConversationItemBg),
            border: Border(
                bottom: BorderSide(
                    color: Color(Appcolors.DividerColor), width: 1.0))),
        padding: EdgeInsets.all(10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
                padding: EdgeInsets.only(right: 10),
                child: _avatarContainer(avatarImg)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(bottom: 10),
                    child: Text(
                      wechatConversation.title,
                      maxLines: 1,
                      style: TextStyle(
                          fontSize: 15,
                          color: Color(wechatConversation.titleColor)),
                    ),
                  ),
                  Container(
                    child: Text(wechatConversation.des,
                        maxLines: 1,
                        style: TextStyle(
                            fontSize: 12,
                            color: Color(Appcolors.ContentTextColor))),
                  ),
                ],
              ),
            ),
            Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    wechatConversation.updateAt,
                    style: TextStyle(
                        fontSize: 10, color: Color(Appcolors.ContentTextColor)),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _deviceInfo extends StatelessWidget {
  const _deviceInfo({Key key, this.device: Device.Mac})
      : assert(device != null);

  final Device device;

  int getTypeImg() {
    return device == Device.Mac ? 0xe662 : 0xe601;
  }

  String getPcName() {
    return device == Device.Mac ? "Mac" : "Windows";
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      color: Color(Appcolors.DividerColor),
      child: Row(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(right: 5),
            child: Icon(
                IconData(getTypeImg(), fontFamily: Constants.IconFontFamily)),
          ),
          Container(
            child: Text(
              getPcName(),
              style: TextStyle(fontSize: 14),
            ),
          ),
          Container(
            child: Text("微信已登录，手机通知已关闭"),
          ),
        ],
      ),
    );
  }
}

class ChatList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _chatListState();
  }
}

class _chatListState extends State<ChatList> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        if (index == 0) {
          return _deviceInfo();
        } else {
          return ChatListPage(
            wechatConversation: mockChatListData[index - 1],
          );
        }
      },
      itemCount: mockChatListData.length - 1,
    );
  }
}
